{% extends 'bms_home.html' %}


{% block content %}
    <h2 class="text-center">图书列表</h2>
    <a href="{% url 'app01:app01_book_add_view' %}" class="btn btn-success">新增图书</a>
    <table class="table table-striped table-hover">
        <thead>
        <tr>
            <th>主键</th>
            <th>书名</th>
            <th>价格</th>
            <th>出版日期</th>
            <th>出版社</th>
            <th>作者</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {% for book_obj in book_query %}
            <tr>
                <td>{{ book_obj.pk }}</td>
                <td>{{ book_obj.title }}</td>
                <td>{{ book_obj.price }}</td>
                <td>{{ book_obj.publish_time|date:'Y-m-d' }}</td>
                <td>{{ book_obj.publish.title }}</td>
                <td>
                    {% for author_obj in book_obj.authors.all %}
                        {% if forloop.last %}
                            {{ author_obj.name }}
                        {% else %}
                            {{ author_obj.name }},
                        {% endif %}
                    {% endfor %}
                </td>
                <td>
{#                    <a href="{% url 'app01:app01_book_edit_view' book_obj.pk %}" class="btn btn-primary btn-xs">编辑</a>#}
{#                    <a href="{% url 'app01:app01_book_delete_view' book_obj.pk %}" class="btn btn-danger btn-xs">删除</a>#}
                    <a href="" id="sub" class="btn btn-primary btn-xs">编辑</a>
                    <a href="" id="sub" class="btn btn-danger btn-xs">删除</a>
                </td>
            </tr>
        {% endfor %}
            <script>
                $("#sub").click(function () {
        swal({
                    title: "你确定要删除吗？",
                    text: "删除可就找不回来了哦！",
                    type: "warning",
                    showCancelButton: true,  // 是否显示取消按钮
                    confirmButtonClass: "btn-danger",  // 确认按钮的样式类
                    confirmButtonText: "删除",  // 确认按钮文本
                    cancelButtonText: "取消",  // 取消按钮文本
                    closeOnConfirm: false,  // 点击确认按钮不关闭弹框
                    showLoaderOnConfirm: true  // 显示正在删除的动画效果
                },
                function () {
                    var deleteId = 2;
                    $.ajax({
                        url: "/book_delete/",
                        type: "post",
                        data: {"id": deleteId},
                        success: function (data) {
                            if (data.code === 0) {
                                swal("删除成功!", "你可以准备跑路了！", "success");
                            } else {
                                swal("删除失败", "你可以再尝试一下！", "error")
                            }
                        }
                    })
                });
            })
            </script>
        </tbody>
    </table>
    <nav aria-label="Page navigation" class="text-center">
        <ul class="pagination">
            <li>
                <a href="#" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
            <li><a href="#">1</a></li>
            <li><a href="#">2</a></li>
            <li><a href="#">3</a></li>
            <li><a href="#">4</a></li>
            <li><a href="#">5</a></li>
            <li>
                <a href="#" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
        </ul>
    </nav>

{% endblock %}

